<?php
namespace application\admin\panels;
use application\utilities\Utility;
use application\models\Box;
class boxes extends panel {
	public $name = 'Boxes';
	
	public static function js() {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript('jquery ui');
		Modulator()->includeScript(getCommonFile('third-party/plupload/js/plupload.full.js'));
		Modulator()->includeScript(getCommonFile('third-party/plupload/js/jquery.plupload.queue/jquery.plupload.queue.js'));
		?>
		<script type="text/javascript" src="<?=getCommonFile('third-party/wymeditor/jquery.wymeditor.js')?>"></script>
		<script type="text/javascript" src="<?=getCommonFile('concerto/wymeditor/concerto-buttons.js')?>"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$('#box-content').wymeditor({
					stylesheet: '<?=getCommonFile('concerto/css/wymeditor.css')?>',
					postInit: function(wym) {
						wym.concertoButtons('<?=CN_URL?>');
 
			            
					}
				});
			});
		</script>
		<?php
	}
	
	public function handleData() {
		if($_REQUEST['edit_box']) {
			$box = new Box(getQueryVar('edit'));
			if($_REQUEST['box_delete']) {
				$box->delete();
				$this->addMessage('success', 'Box successfully deleted.');
				deleteQueryVar('edit');
			}
			else {
				$box->setName($_REQUEST['box_name']);
				$box->setContent($_REQUEST['box_content']);
				$results = $box->save();
				$this->mergeMessages($results);
				if(!$results['errors']) deleteQueryVar('edit');
			}
			
		}
		elseif($_REQUEST['add_box']) {
			$box = new Box;
			$box->create(array('name'=>$_REQUEST['box_name'], 'content'=>$_REQUEST['box_content']));
			$results = $box->save();
			$this->mergeMessages($results);
		}
		
	}
	
	public function display() {
		$this->displayMessages();
		?>
		<div class="one-column">
			<div id="about-boxes" class="box">
				<h3><button class="minimize small-button">-</button>About Boxes</h3>
				<div class="box-content">
					<p>Boxes are small elements of text, images, or other content that you can place in your template with a simple line of code.</p>
					<p>You can add a box into your template just by using <b>displayBox($boxName)</b>.</p>
				</div>
			</div>
			<?php if(getQueryVar('edit') && DB()->getCount("SELECT `box_id` FROM `cn_boxes` WHERE `box_id`='".getQueryVar('edit')."'")) : $box = new Box(getQueryVar('edit'));?>
			<div class="box">
				<h3>Edit Box</h3>
				<form class="box-content" method="post">
					<ul class="form-fields">
						<li>
							<label>Box Name</label>
							<input type="text" class="big-text" value="<?=$box->getName()?>" name="box_name"/>
						</li>
						<li>
							<label>Box Content</label>
							<textarea id="box-content" name="box_content"><?=$box->getContent()?></textarea>
							
						</li>
						<li>
								<label class="field-delete">
									<input type="checkbox" name="box_delete" value="1"/> Delete Box
								</label>
						</li>
					</ul>
					<input type="submit" class="small-button" name="edit_box"/>
				</form>
			</div>
			<?php else:?>
			<div class="box">
				<h3>Add Box</h3>
				<form class="box-content" method="post">
					<ul class="form-fields">
						<li>
							<label>Box Name</label>
							<input type="text" class="big-text" name="box_name"/>
						</li>
						<li>
							<label>Box Content</label>
							<textarea id="box-content" name="box_content"></textarea>
							
						</li>
					</ul>
					<input type="submit" class="small-button" name="add_box"/>
				</form>
			</div>
			<?php endif;?>
			
			

			<div class="box">
				<h3>Boxes (Click to edit)</h3>
				<table class="admin-table" width="100%">
					<thead>
						<tr>
							<th class="first" width="2%"></th>
							<th width="96%">Name</th>
							<th class="last" width="2%"></th>
						</tr>
					</thead>
					<tfoot>
						<tr>
							<th class="first" width="2%"></th>
							<th width="96%">Name</th>
							<th class="last" width="2%"></th>
						</tr>
					</tfoot>
					<tbody>
						<?php $style = '';?>
						<?php $results = DB()->getResults("SELECT * FROM `cn_boxes`");
						foreach($results as $result) : 
						
						?>
						
						<tr<?=$style?>>
							<td class="first"></td>
							
							<td><a href="?display=boxes&edit=<?=$result->box_id?>"><?=$result->box_name?></a></td>
							<td class="last"></td>
						</tr>
						<?php $style = ($style == ' class="alternate"') ? '' : ' class="alternate"';?>
						<?php endforeach;?>
					</tbody>
				</table>
			</div>
		</div>
		<?php
	}
}
?>